package it.biffi.jirc.bot.util;

public class XDCCPopupDecoder {

	// Breaking Bad S05E03 iTALiAN LD 480p WEB-DL x264-DpHs.mkv

	// Bot Nick: SUN|SERIETV-NEWS|02
	// Channel: #SunShiNe
	// Network: Uragano
	//
	// /msg SUN|SERIETV-NEWS|02 xdcc send #510
	//
	// irc://irc.uragano.org/SunShiNe
	/**
	 * Return array of string with 0 = server 1 = channel 2 = bot 3 = packet
	 * 
	 * @param popup
	 * @return
	 */
	public static String[] decode(String popup) {
		System.out.println("decoding: " + popup);
		String server = "";
		String channel = "";
		String bot = "";
		String packet = "";
		int startMsg = popup.indexOf("/msg");

		int startBot = popup.indexOf("Bot Nick: ");
		int botLabeLOffset = "Bot Nick: ".length();
		int startChan = popup.indexOf("Channel: ");
		int startNetwork = popup.indexOf("Network: ");
		bot = popup.substring(startBot + botLabeLOffset, startChan).trim();
		System.out.println(bot);
		int channelLabeLOffset = "Channel: ".length();
		channel = popup.substring(startChan + channelLabeLOffset + 1,
				startNetwork).trim();
		System.out.println(channel);

		String residue = popup.substring(startMsg, popup.length());
		int startPacket = residue.indexOf("#");
		int startIrcServer = residue.indexOf("irc://");
		packet = residue.substring(startPacket + 1, startIrcServer).trim();
		System.out.println(residue);
		System.out.println(packet);
		String url = residue.substring(startIrcServer, residue.length());
		System.out.println(url);
		String[] splitted = url.split("/");
		// for (String string : splitted) {
		// System.out.println(string);
		// }
		server = splitted[2];
		System.out.println(server);
		return new String[] { server, channel, bot, packet };

	}

	public static void main(String[] args) {
		String test = "Bot Nick: SUN|SERIETV-NEWS|02  Channel: #SunShiNe	 Network: Uragano		 /msg SUN|SERIETV-NEWS|02 xdcc send #510  irc://irc.uragano.org/SunShiNe";

		XDCCPopupDecoder.decode(test);
	}
}
